<?php

/**
 * 公共服务
 */

namespace App\Tools;
use Illuminate\Support\Facades\Redis;
class ApiService
{
    public static $timeOut = 8640000;
    public static $appid = '1437166917';
    public static $appsecret = 'W8aI6KohDHEtuBhh';

    /**
     * 验证签名
     * @param $sign
     * @param int $timestamp
     * @return bool
     */
    public static function appSignVerify($sign, $timestamp = 0)
    {
        if ($sign && $timestamp) {
            $appid = self::$appid;
            $appsecret = self::$appsecret;
            $newSign = md5("appid={$appid}&appsecret={$appsecret}&timestamp={$timestamp}");
            if ($newSign == $sign) {
                return true;
            }
        }
        return false;
    }

    /**
     * 登录信息
     * @param $dataArr
     */
    public static function appTokenLogin($user_id, $dataArr)
    {
        if ($user_id && $dataArr) {
            $tokenKey = md5("Token:{$user_id}:" . time());
            Redis::SET('XCX_TOKEN:'.$tokenKey, json_encode($dataArr, JSON_UNESCAPED_UNICODE));
            Redis::EXPIRE('XCX_TOKEN:'.$tokenKey, self::$timeOut);
            return $tokenKey;
        }
        return false;
    }

}
